/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package library.Choosers;

import java.io.File;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.SwingUtilities;
import javax.swing.JFileChooser;
import java.awt.Component;

import org.apache.log4j.*;

import library.Configuration.*;

/**
 * This class implements a directory chooser.
 * @author JNC
 */
@SuppressWarnings("serial")
public class DirectoryChooser extends JFileChooser {
    
    /**
     * The log4j log/
     */
    private static final Logger _logger = Logger.getLogger(DirectoryChooser.class);

    /**
     * Proposed default directory
     */
    private final String _DEFAULT_DIRECTORY = Configuration.getPathProperty("AnyForm.DefaultDirectory") ;
    
    /**
     * The keyword to show on the button.
     */
    private static final String _KEYWORD_SELECT = "Select";
    /**
     * The title to show on the window.
     */
    private static final String _TITLE = "Select the directory to start processing";    
    
    /**
     * Constructor.
     * By default, use _DEFAULT_DIRECTORY as default directory.
     */    
    public DirectoryChooser () {
        initialize(_DEFAULT_DIRECTORY);
    }
    
    /**
     * Constructor.
     * @param myDirectoryPath
     * The directory path to use.
     */
    public DirectoryChooser(String myDirectoryPath) {
        initialize(myDirectoryPath);
    }
    
    /**
     * Performs all the necessary actions to build the Class_DirectoryChooser.
     * Performs the following actions :
     * <ul>
     * <li> changes the look and feel,
     * <li> sets the titile on the window,
     * <li> sets the text on the "Approve" button,
     * <li> indicates that only directories can be selected,
     * <li> sets the default directory.
     * </ul>
     * @param myDirectoryPath
     * The directory path to use.
     */
    private void initialize(String myDirectoryPath) {
        // Change the look and feel to Windows Classic Look and Feel
        changeToWindowsLookAndFeel();
    
        // set the title
        this.setDialogTitle(_TITLE);
        
        // change the text for the select button
        setApproveButtonText(_KEYWORD_SELECT);
                
        // indicates that the dialog shoudl let select directories and not files
        setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        
        // set the default directory
        setCurrentDirectory(new File(myDirectoryPath));
    }
    
    /**
     * Show the frame and returns the selected directory.
     * @param myjParent
     * Parent component.
     * @return
     * Returns the selected directory path.
     */
    public String showOpenDialogAndReturnDirectoryPath(Component myjParent) {
        int iReturn;
        String sReturn = null;
        
        iReturn = showOpenDialog(myjParent);
        
        switch(iReturn) {
            case APPROVE_OPTION :
                _logger.debug("Selected directory = " + super.getSelectedFile().getAbsolutePath());
                sReturn = getSelectedFile().getAbsolutePath();
                break;
            case CANCEL_OPTION :
                _logger.warn("Action canceled by the user");
                break;
            default :
                _logger.warn("Unknown return for showOpenDialog : " + iReturn);
                break;
        }
        return sReturn;
    }       
    
    /**
     * Changes the look and feel
     */
    private void changeToWindowsLookAndFeel() {
                
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
        }
        catch (InstantiationException e) {
            _logger.debug("Exception InstantiationException caught");
        }
        catch (ClassNotFoundException e) {
            _logger.debug("Exception ClassNotFoundException caught");
        }
        catch (UnsupportedLookAndFeelException e) {
            _logger.debug("Exception UnsupportedLookAndFeelException caught");
        }
        catch (IllegalAccessException e) {
            _logger.debug("Exception IllegalAccessException caught");
        }
    }
}
